package com.arcbbs.service.impl;

import com.arcbbs.entity.SysSetting;
import com.arcbbs.entity.SysSettingDto;
import com.arcbbs.entity.SystemSettingAduitDto;
import com.arcbbs.entity.SystemSettingCommentDto;
import com.arcbbs.enums.SysSettingCodeEnum;
import com.arcbbs.mapper.SysSettingMapper;
import com.arcbbs.service.SysSettingService;
import com.arcbbs.utils.JsonUtils;
import com.arcbbs.utils.StringTools;
import com.arcbbs.utils.SysCacheUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.List;


@Service
public class SysSettingServiceImpl implements SysSettingService {
    private static final Logger logger = LoggerFactory.getLogger(SysSettingServiceImpl.class);
    @Resource
    private SysSettingMapper sysSettingMapper;

    public void refresCache() {
        System.out.println("xxxx");
        try {
            SysSettingDto sysSettingDto = new SysSettingDto();
//获取系统设置全部列表
            List<SysSetting> list = sysSettingMapper.selectList();
//循环
            for (SysSetting sysSetting : list) {
     //获取每个实体类的jsoncontent
                String jsonContent = sysSetting.getJsonContent();
                System.out.println(jsonContent);

                if (StringTools.isEmpty(jsonContent)) {
                    continue;
                }
              //获取实体类中的code
                String code = sysSetting.getCode();
                System.out.println(code+"code");
                //通过code获取枚举
                SysSettingCodeEnum sysSettingCodeEnum = SysSettingCodeEnum.getByCode(code);
                System.out.println(sysSettingCodeEnum+"sys");
                //反射
                PropertyDescriptor pd = new PropertyDescriptor(sysSettingCodeEnum.getPropName(), SysSettingDto.class);
                System.out.println(pd + "po");
                //写入属性值
                Method method = pd.getWriteMethod();
                Class subclass = Class.forName(sysSettingCodeEnum.getClassz());
                //对各个子类进行重写
                method.invoke(sysSettingDto, JsonUtils.convertJSON2Obj(jsonContent, subclass));
            }
            logger.info(JsonUtils.convertObjJson(sysSettingDto));
            //刷新缓存
            SysCacheUtils.refres(sysSettingDto);
        } catch (Exception e) {
            logger.error("刷新缓存失败", e);
        }
    }

}
